package com.sander.guardmanagement.ui.activity;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.http.UserSubmitSuggest;
import com.sander.guardmanagement.response.ResponseModel;
import com.sander.guardmanagement.until.UserInfoManager;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

public class FeedbackActivity extends AppCompatActivity {

    public EditText inputView;
    public int index;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initCallBack();
        initData();
        initView();
    }
    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void initData(){

    }
    private void initView(){
        Button button = (Button) findViewById(R.id.feedback_button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButtonIndex(0);
            }
        });
        Button button1 = (Button) findViewById(R.id.feedback_button2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButtonIndex(1);

            }
        });

        Button button2 = (Button) findViewById(R.id.feedback_button3);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButtonIndex(2);

            }
        });

        inputView = (EditText ) findViewById(R.id.inputcontentview);


        Button okButton = (Button) findViewById(R.id.sureqpplycooperation);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureText();
            }
        });

    }

    public void setButtonIndex(int index){
        Button button = (Button) findViewById(R.id.feedback_button1);
        Button button1 = (Button) findViewById(R.id.feedback_button2);
        Button button2 = (Button) findViewById(R.id.feedback_button3);

        this.index = index;
        Resources res = getResources( );
        Drawable shape = res.getDrawable(R.drawable.shape);
        Drawable grayshape = res.getDrawable(R.drawable.grayshape);
        if (this.index == 0){

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                button.setBackground(getResources().getDrawable(R.drawable.shape,null));
                button1.setBackground(getResources().getDrawable(R.drawable.grayshape,null));
                button2.setBackground(getResources().getDrawable(R.drawable.grayshape,null));
            }else{
                button.setBackground(shape);
                button1.setBackground(grayshape);
                button2.setBackground(grayshape);
            }

        }else if (this.index == 1){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                button.setBackground(getResources().getDrawable(R.drawable.grayshape,null));
                button1.setBackground(getResources().getDrawable(R.drawable.shape,null));
                button2.setBackground(getResources().getDrawable(R.drawable.grayshape,null));
            }else{
                button.setBackground(grayshape);
                button1.setBackground(shape);
                button2.setBackground(grayshape);
            }
        }else if (this.index == 2){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                button.setBackground(getResources().getDrawable(R.drawable.grayshape,null));
                button1.setBackground(getResources().getDrawable(R.drawable.grayshape,null));
                button2.setBackground(getResources().getDrawable(R.drawable.shape,null));
            }else{
                button.setBackground(grayshape);
                button1.setBackground(grayshape);
                button2.setBackground(shape);
            }
        }

    }

    public void sureText(){
        String text = inputView.getText().toString();
        if (TextUtils.isEmpty(text)){
            Toast.makeText(getApplicationContext(),"建议为空",Toast.LENGTH_SHORT);
        }

        UserSubmitSuggest submitSuggest = new UserSubmitSuggest();
        submitSuggest.uid = UserInfoManager.sharedManager().getUserInfo().getId() + "";
        submitSuggest.suggest_type = this.index + 1 + "";
        submitSuggest.suggest_content = text;

        OkHttpHelper.getinstance().post(submitSuggest.requestUrl(), submitSuggest.getValueOfClas(), new BaseCallback<ResponseModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseModel o) {
                if (o.getCode() == 0){
                    Toast.makeText(getApplicationContext(),"提交成功",Toast.LENGTH_SHORT);
                    finish();
                }else{
                    Toast.makeText(getApplicationContext(),o.getMsg(),Toast.LENGTH_SHORT);
                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });

    }


}
